﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Specialized;
using System.IO;
using System.Xml.Serialization;

namespace SqlDrive
{
    public static class HtmlContentExtn
    {
        public static HtmlContent ToHtmlContent(this string content)
        {
            return new HtmlContent(content);
        }
        public static string ToString(this HtmlContent html)
        {
            return HtmlContent.ToString(html.Contents);
        }
    }
    public class HtmlContent
    {
        public StringDictionary Contents { get; set; }

        public HtmlContent(string content)
        {
            Contents = ToDictionary(content);
        }
        public HtmlContent(StringDictionary html)
        {
            Contents = html;
        }
        public HtmlContent(Dictionary<string, string> keyValues)
        {
            Contents = new StringDictionary();            

            foreach (var kp in keyValues)
            {
                Contents.Add(kp.Key, kp.Value);
            }
        }
        public static StringDictionary ToDictionary(string serializedContent)
        {
            StringDictionary cont = null;
            StringReader reader = new StringReader(serializedContent);
            try
            {
                XmlSerializer srz = new XmlSerializer(typeof(StringDictionary));
                cont = (StringDictionary)srz.Deserialize(reader);
            }
            finally
            {
                reader.Close();
            }
            return cont;
        }
        public static string ToString(StringDictionary contents)
        {
            StringBuilder sb = new StringBuilder();
            StringWriter writer = new StringWriter(sb);
            try
            {
                XmlSerializer srz = new XmlSerializer(typeof(StringDictionary));
                srz.Serialize( writer, contents);
            }
            finally
            {
                writer.Close();
            }
            return sb.ToString();
        }
    }
}
